package com.mobimagic.lockscreen.util;

import java.util.List;

import com.mobimagic.lockscreen.AppEnv;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.Log;

/**
 * @file NetworkUtils.java
 *
 * @author liule-ms
 *
 * @date 2015年1月6日 上午10:55:30
 *
 * @description
 */
public class NetworkUtils {
    private static final boolean DEBUG = AppEnv.bAppdebug;
    private static final String TAG = "NetworkUtils";

    public static final int NETWORK_CLASS_NO_NETWORK = 0;
    public static final int NETWORK_CLASS_WIFI = 1;
    public static final int NETWORK_CLASS_2_G = 2;
    public static final int NETWORK_CLASS_3_G = 3;
    public static final int NETWORK_CLASS_4_G = 4;
    public static final int NETWORK_CLASS_UNKNOW = 5;

    /**
     * 判断数据连接是否可用
     *
     * @param context
     * @return
     */
    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager manager = (ConnectivityManager) NetworkUtils.getSystemService(context,
                Context.CONNECTIVITY_SERVICE);
        if (manager != null) {
            NetworkInfo networkInfo = manager.getActiveNetworkInfo();
            return networkInfo != null && networkInfo.isConnected();
        }
        return false;
    }

    /**
     * 判断wifi是否打开
     *
     * @param context
     * @return
     */
    public static boolean isWifiEnabled(Context context) {
        WifiManager manager = (WifiManager) NetworkUtils.getSystemService(context, Context.WIFI_SERVICE);
        return manager != null && manager.isWifiEnabled();
    }

    /**
     * 判断无线是否可见
     *
     * @param context
     * @return
     */
    public static boolean isWifiAvailable(Context context) {
        boolean isAvailable = false;
        ConnectivityManager manager = (ConnectivityManager) NetworkUtils.getSystemService(context,
                Context.CONNECTIVITY_SERVICE);
        if (manager != null) {
            NetworkInfo networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            if (networkInfo != null) {
                State state = networkInfo.getState();
                if (DEBUG) {
                    Log.e(TAG, "state = " + state);
                }
                if (State.DISCONNECTED == state || State.UNKNOWN == state) {
                    isAvailable = false;
                } else {
                    isAvailable = networkInfo.isAvailable();
                }
            }
        }
        if (DEBUG) {
            Log.e(TAG, "isWifiAvailable = " + isAvailable);
        }
        return isAvailable;
    }

    /**
     * 当前网络是否是wifi
     *
     * @param context
     * @return
     */
    public static boolean isWifiConnected(Context context) {
        ConnectivityManager manager = (ConnectivityManager) NetworkUtils.getSystemService(context,
                Context.CONNECTIVITY_SERVICE);
        if (manager != null) {
            NetworkInfo networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            return networkInfo != null && networkInfo.isConnected();
        }
        return false;
    }

    /**
     * 封装这个方法是因为保证使用Application context来调用getSystemService，否则会内存泄漏
     *
     * @param context
     * @param name
     * @return
     */
    public static Object getSystemService(Context context, String name) {
        return context.getApplicationContext().getSystemService(name);
    }

    /**
     * 跳转网络设置界面
     *
     * @param context
     */
    public static void skipToNetworkSetting(Context context) {
        Intent intent = new Intent();
        if (NetworkUtils.isWifiEnabled(context)) {
            intent.setAction(Settings.ACTION_DATA_ROAMING_SETTINGS);
        } else {
            intent.setAction(Settings.ACTION_WIFI_SETTINGS);
        }
        //检查系统是否能处理action，如果不行，跳到ACTION_SETTING，如果ACTION_SETTING都不行，那就不跳转了，直接返回
        if (NetworkUtils.isSupportIntent(context, intent)) {
            context.startActivity(intent);
        } else {
            intent.setAction(Settings.ACTION_SETTINGS);
            if (NetworkUtils.isSupportIntent(context, intent)) {
                context.startActivity(intent);
            }
        }
    }

    /**
     * 判断系统中是否有能处理intent的activity， 有返回true，没有返回false
     *
     * @param context
     * @param intent
     * @return
     */
    private static boolean isSupportIntent(final Context context, final Intent intent) {
        boolean ret = false;
        PackageManager packageManager = context.getPackageManager();
        List<ResolveInfo> resolveInfos = packageManager
                .queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        if (resolveInfos != null && resolveInfos.size() > 0) {
            ret = true;
        }
        return ret;
    }

    /**
     * 获取当前网络类型
     *
     * @param context
     * @return
     */
    public static int getNetWorkType(Context context) {
        ConnectivityManager manager = (ConnectivityManager) NetworkUtils.getSystemService(context,
                Context.CONNECTIVITY_SERVICE);
        boolean isConnected = false;
        if (manager != null) {
            NetworkInfo[] networkInfos = manager.getAllNetworkInfo();
            if (networkInfos != null) {
                for (NetworkInfo networkInfo : networkInfos) {
                    if (networkInfo != null && networkInfo.isConnected()) {
                        isConnected = true;
                    }
                }
            }
        }
        if (!isConnected) {
            return NETWORK_CLASS_NO_NETWORK;
        } else {
            if (manager != null) {
                NetworkInfo networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                if (networkInfo != null && networkInfo.isConnected()) {
                    return NETWORK_CLASS_WIFI;
                }
            }
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            int networkType = telephonyManager.getNetworkType();
            return NetworkUtils.getMobileNetWorkClass(networkType);
        }
    }

    /**
     * 获取手机网络类型，非wifi网络
     *
     * @param networkType
     * @return
     */
    private static int getMobileNetWorkClass(int networkType) {
        switch (networkType) {
        case TelephonyManager.NETWORK_TYPE_GPRS:
        case TelephonyManager.NETWORK_TYPE_EDGE:
        case TelephonyManager.NETWORK_TYPE_CDMA:
        case TelephonyManager.NETWORK_TYPE_1xRTT:
        case TelephonyManager.NETWORK_TYPE_IDEN:
            return NETWORK_CLASS_2_G;
        case TelephonyManager.NETWORK_TYPE_UMTS:
        case TelephonyManager.NETWORK_TYPE_EVDO_0:
        case TelephonyManager.NETWORK_TYPE_EVDO_A:
        case TelephonyManager.NETWORK_TYPE_HSDPA:
        case TelephonyManager.NETWORK_TYPE_HSUPA:
        case TelephonyManager.NETWORK_TYPE_HSPA:
        case TelephonyManager.NETWORK_TYPE_EVDO_B:
        case TelephonyManager.NETWORK_TYPE_EHRPD:
        case TelephonyManager.NETWORK_TYPE_HSPAP:
            return NETWORK_CLASS_3_G;
        case TelephonyManager.NETWORK_TYPE_LTE:
            return NETWORK_CLASS_4_G;
        default:
            return NETWORK_CLASS_UNKNOW;
        }
    }
}
